package com.qzsoft.zhjd.server.modules.asphalt.service.impl;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import com.qzsoft.zhjd.server.modules.asphalt.handler.AsphaltAlarmHandler;
import com.qzsoft.zhjd.server.modules.asphalt.handler.AsphaltDataHandler;
import com.qzsoft.zhjd.server.modules.asphalt.handler.AsphaltDataRatioHandler;
import com.qzsoft.zhjd.server.modules.asphalt.mapper.CollectAsphaltDataWeightMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltAlarmEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataRatioEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataSieveEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataWeightEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchColdEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchHotEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchThresholdEntity;
import com.qzsoft.zhjd.server.modules.asphalt.handler.AsphaltDataSieveHandler;
import com.qzsoft.zhjd.server.modules.asphalt.handler.AsphaltDataWeightHandler;
import com.qzsoft.zhjd.server.modules.asphalt.mapper.CollectAsphaltAlarmMapper;
import com.qzsoft.zhjd.server.modules.asphalt.mapper.CollectAsphaltDataMapper;
import com.qzsoft.zhjd.server.modules.asphalt.mapper.CollectAsphaltDataRatioMapper;
import com.qzsoft.zhjd.server.modules.asphalt.mapper.CollectAsphaltDataSieveMapper;
import com.qzsoft.zhjd.server.modules.asphalt.model.CollectDataModel;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltMatchColdService;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltMatchHotService;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltMatchService;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltMatchThresholdService;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltMatchTypeService;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltReceiveService;

@Service
public class CollectAsphaltReceiveServiceImpl implements CollectAsphaltReceiveService {

    @Autowired
    private CollectAsphaltMatchService collectAsphaltMatchService;

    @Autowired
    private CollectAsphaltMatchTypeService collectAsphaltMatchTypeService;

    @Autowired
    private CollectAsphaltMatchHotService collectAsphaltMatchHotService;

    @Autowired
    private CollectAsphaltMatchColdService collectAsphaltMatchColdService;

    @Autowired
    private CollectAsphaltMatchThresholdService collectAsphaltMatchThresholdService;

    @Autowired
    private CollectAsphaltDataMapper collectAsphaltDataMapper;

    @Autowired
    private CollectAsphaltAlarmMapper collectAsphaltAlarmMapper;

    @Autowired
    private CollectAsphaltDataSieveMapper collectAsphaltDataSieveMapper;

    @Autowired
    private CollectAsphaltDataWeightMapper collectAsphaltDataWeightMapper;

    @Autowired
    private CollectAsphaltDataRatioMapper collectAsphaltDataRatioMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void receive(CollectDataModel model) {
        // 删除已经存在的数据
        String stationCode = model.getStationCode();
        String originalId = model.getId();
        deleteByOriginalId(stationCode, originalId);

        // 获取拌合站信息
        String matchCode = model.getMatchCode();
        CollectAsphaltMatchEntity match = collectAsphaltMatchService.getByStationAndCode(stationCode, matchCode);

        // 获取热料配置信息
        List<CollectAsphaltMatchHotEntity> hots = null;
        // 获取上下限信息
        CollectAsphaltMatchThresholdEntity threshold = null;
        // 获取冷料配比
        CollectAsphaltMatchColdEntity cold = null;
        // 获取筛孔列表
        Set<String> sieveSet = null;
        if (null != match) {
            hots = collectAsphaltMatchHotService.listByMatchId(match.getId());
            threshold = collectAsphaltMatchThresholdService.getByTypeAndName(match.getMatchType(), match.getMatchName());
            cold = collectAsphaltMatchColdService.getById(match.getId());
            sieveSet = collectAsphaltMatchTypeService.getSieveSetByType(match.getMatchType());
        }

        // 数据主键
        String id = UUID.randomUUID().toString().replace("-", "");

        // 处理生产数据重量表
        CollectAsphaltDataWeightEntity weightData = AsphaltDataWeightHandler.coverTo(model, match, cold);
        weightData.setId(id);

        // 处理生产数据占比表
        CollectAsphaltDataRatioEntity ratioData = AsphaltDataRatioHandler.coverTo(weightData);
        ratioData.setId(id);

        // 处理筛孔数据
        CollectAsphaltDataSieveEntity sieveData = AsphaltDataSieveHandler.coverTo(weightData, ratioData, hots, sieveSet);
        sieveData.setId(id);

        // 处理生产数据主表
        CollectAsphaltDataEntity data = AsphaltDataHandler.coverToData(model, match);
        data.setId(id);
        data.setOriginalId(originalId);
        data.setAsphaltAggregateRatio(ratioData.getAsphaltRatioW());

        // 处理预警数据
        CollectAsphaltAlarmEntity alarm = AsphaltAlarmHandler.coverTo(data, sieveData, match, threshold);
        alarm.setId(id);

        // 执行添加操作
        collectAsphaltDataMapper.insert(data);
        collectAsphaltDataSieveMapper.insert(sieveData);
        collectAsphaltDataWeightMapper.insert(weightData);
        collectAsphaltDataRatioMapper.insert(ratioData);
        collectAsphaltAlarmMapper.insert(alarm);
    }

    /**
     * @Title deleteByOriginalId
     * @Description 根据原始主键删除已经存在的数据
     * @param stationCode 拌合楼编号
     * @param originalId 原始主键
     */
    private void deleteByOriginalId(String stationCode, String originalId) {
        // 如果原始主键为空，不做处理
        if (ObjectUtils.isEmpty(originalId) || ObjectUtils.isEmpty(stationCode)) {
            return;
        }

        // 使用原始主键查询数据
        QueryWrapper<CollectAsphaltDataEntity> queryWrapper = new QueryWrapper<CollectAsphaltDataEntity>();
        queryWrapper.eq("station_code", stationCode);
        queryWrapper.eq("original_id", originalId);
        List<CollectAsphaltDataEntity> list = collectAsphaltDataMapper.selectList(queryWrapper);
        if (null == list || 0 == list.size()) {
            return;
        }

        // 删除已经存在的数据
        String id = list.get(0).getId();
        collectAsphaltDataMapper.deleteById(id);
        collectAsphaltDataSieveMapper.deleteById(id);
        collectAsphaltDataWeightMapper.deleteById(id);
        collectAsphaltDataRatioMapper.deleteById(id);
        collectAsphaltAlarmMapper.deleteById(id);
    }
}
